package com.arpgalaxy.generator.utils;

import com.arpgalaxy.generator.entity.Properties;
import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.StringTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

/**
 * @author arpgalaxy
 * @email 13605560342@163.com
 * @date 2021/3/11 0011
 * @Description
 */
public class FreemarkerUtils {
    /**
     * 获取配置类
     */
    public static Configuration getConfiguration(){
//1.创建freemarker配置类
        Version v = new Version("2.3.31");
        return getConfiguration(v);
    }
    public static Configuration getConfiguration(Version version ){
//1.创建freemarker配置类
        Configuration configuration = new Configuration(version);
        TemplateLoader[] loaders=null;

        FileTemplateLoader ftl1 = null;
        try {
            ftl1 = new FileTemplateLoader(new File("").getAbsoluteFile());
        } catch (IOException e) {
            e.printStackTrace();
        }

        StringTemplateLoader st = new StringTemplateLoader();
        loaders = new TemplateLoader[] { ftl1, st};
        MultiTemplateLoader mtl = new MultiTemplateLoader(loaders);
        configuration.setTemplateLoader(mtl);
        return configuration;
    }
    public static String executeStringGenerator(Configuration configuration,Object dataModel,String str){
        try {
            Template template = new Template(str,str,configuration);
            StringWriter stringWriter = new StringWriter();
            template.process(dataModel,stringWriter);
            return stringWriter.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (TemplateException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static String executePathGenerator(Configuration configuration,Object dataModel,Path path){
        String patName = executeStringGenerator(configuration,dataModel,path.toString());
        return patName;
    }
    public static boolean executeFileGenerator(Configuration configuration, Properties properties,Map<String,String> dataModel, Path path){
        Template t = null;
        String  fileName = executePathGenerator(configuration,dataModel,path.getFileName());
        String  dir = executePathGenerator(configuration,dataModel,path.getParent());
        Path targetPath = FileUtils.tempPathToTarget(dir, properties);
        if (fileName.toString().endsWith(".java") || fileName.toString().endsWith("Mapper.xml")){
            fileName = StringUtils.firstToUpperCase(fileName);
        }
        //首字母大写后的文件名
        Path p = Paths.get(targetPath.toString(), fileName);
        try {
            t = configuration.getTemplate(path.toString());
            t.process(dataModel,new FileWriter(p.toFile()));
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } catch (TemplateException e) {
            e.printStackTrace();
            return false;
        }
    }

}
